<template>
  <!--
    	作者：wangxw
    	时间：2019-10-26
    	描述：产品分类-添加
    -->
  <el-dialog title="商品满减设置" v-model="dialogVisible" @close="dialogFormVisible" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form size="small" :model="form" ref="form" :rules="rules">
      <el-form-item label="商品名称" :label-width="formLabelWidth">
        <span>{{ productModel.productName }}</span>
      </el-form-item>
      <el-form-item label="是否参与活动" :label-width="formLabelWidth">
        <el-radio-group v-model="form.isJoin">
          <el-radio :label="1">参与</el-radio>
          <el-radio :label="0">不参与</el-radio>
        </el-radio-group>
      </el-form-item>
      <template v-if="form.isJoin == 1">
        <el-form-item label="满类型" :label-width="formLabelWidth" prop="fullType" :rules="[{ required: true, message: ' ' }]">
          <div>
            <el-radio v-model="form.fullType" :label="1">满金额</el-radio>
            <el-radio v-model="form.fullType" :label="2">满件数</el-radio>
          </div>
        </el-form-item>
        <el-form-item label="减类型" :label-width="formLabelWidth" prop="reduceType" :rules="[{ required: true, message: ' ' }]">
          <div>
            <el-radio v-model="form.reduceType" :label="1">减金额</el-radio>
            <el-radio v-model="form.reduceType" :label="2">打折</el-radio>
          </div>
        </el-form-item>
        <el-form-item label="满减设置：" :label-width="formLabelWidth" prop="reduceList" :rules="[{ required: true, message: ' ' }]">
          <div class="tips">如果是打折，填写30，表示减去30，打7折，订单为原价的70%</div>
          <el-table :data="form.reduceList" style="width: 100%">
            <el-table-column label="满值">
              <template #default="scope">
                <el-input type="number" :precision="1" :step="1" :min="0" placeholder="请输入满值" v-model="scope.row.fullValue"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="减值">
              <template #default="scope">
                <el-input type="number" :precision="1" :step="1" :min="0" placeholder="请输入满值" v-model="scope.row.reduceValue"></el-input>
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template #default="scope">
                <el-link type="primary" @click="delReduce(scope.$index)">删除</el-link>
              </template>
            </el-table-column>
          </el-table>
          <el-link type="primary" @click="addReduce()">添加</el-link>
        </el-form-item>
      </template>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible(false)">取 消</el-button>
        <el-button type="primary" @click="saveProduct" :loading="loading">确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import FullreduceApi from '@/api/fullreduce.js';
export default {
  data() {
    var validateActivity = (rule, value, callback) => {
      if (value.length > 0) {
        callback();
      } else {
        callback(new Error('请添加满减规则'));
      }
    };
    return {
      form: {
        isJoin: 0,
        /*满类型*/
        fullType: 1,
        /*减类型*/
        reduceType: 1,
        reduceList: []
      },
      /*左边长度*/
      formLabelWidth: '120px',
      /*是否显示*/
      dialogVisible: false,
      loading: false,
      rules: {
        reduceList: [{ validator: validateActivity, trigger: 'blur' }]
      }
    };
  },
  props: ['open_settings', 'productModel'],
  created() {
    let self = this;
    self.form.isJoin = self.productModel.reducePid != null ? 1 : 0;
    if (self.form.isJoin == 1) {
      self.productModel.reduceList.forEach(function(item) {
        self.form.reduceList.push({
          fullValue: item.fullValue,
          reduceValue: item.reduceValue
        });
      });
      if ((self.form.fullType = self.productModel.reduceList.length > 0)) {
        self.form.fullType = self.productModel.reduceList[0].fullType;
        self.form.reduceType = self.productModel.reduceList[0].reduceType;
      } else {
        self.form.fullType = 1;
        self.form.reduceType = 1;
      }
    }
    self.dialogVisible = self.open_settings;
  },
  methods: {
    /*保存*/
    saveProduct() {
      let self = this;
      let params = self.form;
      params.productId = self.productModel.productId;
      this.$refs.form.validate(valid => {
        if (valid) {
          self.loading = true;
          FullreduceApi.editProduct(params)
            .then(data => {
              self.loading = false;
              ElMessage({
                message: data.msg,
                type: 'success'
              });
              self.dialogFormVisible(true);
            })
            .catch(error => {
              self.loading = false;
            });
        } else {
          ElMessage.error('请添加满减规则');
        }
      });
    },
    /*关闭弹窗*/
    dialogFormVisible(e) {
      if (e) {
        this.$emit('closeDialog', {
          type: 'success',
          openDialog: false
        });
      } else {
        this.$emit('closeDialog', {
          type: 'error',
          openDialog: false
        });
      }
    },
    addReduce() {
      this.form.reduceList.push({
        fullValue: '',
        reduceValue: ''
      });
    },
    delReduce(index) {
      this.form.reduceList.splice(index, 1);
    }
  }
};
</script>

<style scoped>
.img {
  margin-top: 10px;
}
</style>
